package com.mew.user.gold;

import com.mew.common.config.RedisConstantValue;
import com.mew.common.dc.db.ProfileService;
import com.mew.common.dc.db.UserService;
import com.mew.common.entity.user.User;
import com.mew.common.rest.gold.CustomerResponse;
import com.mew.common.rest.gold.CustomersMessage;
import com.mew.common.rest.gold.TranRecordResponse;
import com.mew.common.rest.gold.TranResponse;
import com.mew.common.vo.web.CustomerVo;
import com.mew.user.service.TransferRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/tran")
public class TransferRecordController {

    @Autowired
    private UserService userService;

    @Autowired
    private TransferRecordService transferRecordService;

    @Autowired
    private RedisTemplate<String ,Object> redisTemplate;

    @RequestMapping("/customers")
    @ResponseBody
    public CustomerResponse customers(){
        CustomerResponse message = new CustomerResponse();
        List<CustomersMessage> customers = new ArrayList<>();
        for (Object o : redisTemplate.opsForHash().values(RedisConstantValue.USER_CUSTOMER)) {
            CustomerVo customer = (CustomerVo) o;
            User user = userService.getRedisUserById(customer.getUid());
            if(null != user){
                customers.add(CustomersMessage.builder()
                        .userId(user.getId())
                        .gid(user.getGid())
                        .headPath(user.getHeadPath())
                        .nickName(user.getNickname())
                        .phone(user.getTelNumber())
                        .email(customer.getEmail())
                        .line(customer.getLine())
                        .build());
            }
        }
        message.setCustomersMessageList(customers);
        return message;
    }

    @RequestMapping("/customerInfo/{uid}/{toUid}")
    @ResponseBody
    public int customerInfo(@RequestParam("uid")long uid, @RequestParam("toUid")long toUid){

        return transferRecordService.customerInfo(uid,toUid);
    }

    @GetMapping(value = "/record/{uid}/{tranType}")
    @ResponseBody
    TranRecordResponse tranRecord(@RequestParam("uid")long uid, @RequestParam("tranType")int tranType){
        return transferRecordService.tranRecord(uid,tranType);
    }
}
